package com.lj.dao;

import com.lj.domain.ParamsInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by dave_suwei on 2017/12/9.
 */
public class ParamsInfoDao {
    private SqliteHelper sh;

    public ParamsInfoDao(SqliteHelper sh) {
        this.sh = sh;
    }

    public boolean insertParams(ParamsInfo paramsInfo){
        if(sh==null){
            return false;
        }
        String sqlStr="INSERT INTO t_params_info (showTime,lastSelect,showCount,rejectCount,considerCount,startDate,endDate,rejectInterval,considerInterval) " +
                "VALUES ("+paramsInfo.getShowTime()+","+paramsInfo.getLastSelect()+","+paramsInfo.getShowCount()+","+paramsInfo.getRejectCount()+","+paramsInfo.getConsiderCount()+
                ",\""+paramsInfo.getStartDate()+"\",\""+paramsInfo.getEndDate()+"\","+paramsInfo.getRejectInterval()+","+paramsInfo.getConsiderInterval()+");";
        System.out.println(sqlStr);
        try {
            int r=sh.executeUpdate(sqlStr);
            if(r!=1){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean updateParams(ParamsInfo paramsInfo){
        if(sh==null){
            return false;
        }
        String sqlStr="UPDATE t_params_info set showTime="+paramsInfo.getShowTime()+",lastSelect="+paramsInfo.getLastSelect()+
                ",showCount="+paramsInfo.getShowCount()+",rejectCount="+paramsInfo.getRejectCount()+",considerCount="+paramsInfo.getConsiderCount()+"";
        System.out.println(sqlStr);
        try {
            int r=sh.executeUpdate(sqlStr);
            if(r!=1){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }

    public ParamsInfo getParams(){
        if(sh==null){
            return null;
        }
        ParamsInfo paramsInfo=null;
        String sqlStr="SELECT id,showTime,lastSelect,showCount,rejectCount,considerCount,startDate,endDate,rejectInterval,considerInterval FROM t_params_info limit 1";
        System.out.println(sqlStr);
        try {
            List<ParamsInfo> paramsInfos = sh.executeQuery(sqlStr, new RowMapper<ParamsInfo>() {
                @Override
                public ParamsInfo mapRow(ResultSet rs, int index) throws SQLException {
                    ParamsInfo paramsInfo=new ParamsInfo();
                    paramsInfo.setShowTime(rs.getInt(1));
                    paramsInfo.setLastSelect(rs.getInt(2));
                    paramsInfo.setShowCount(rs.getInt(3));
                    paramsInfo.setRejectCount(rs.getInt(4));
                    paramsInfo.setConsiderCount(rs.getInt(5));
                    paramsInfo.setStartDate(rs.getString(6));
                    paramsInfo.setEndDate(rs.getString(7));
                    paramsInfo.setRejectInterval(rs.getInt(8));
                    paramsInfo.setConsiderInterval(rs.getInt(9));
                    return paramsInfo;
                }
            });
            if(paramsInfos.size()>0){
                paramsInfo=paramsInfos.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return paramsInfo;
    }

    public boolean setParams(ParamsInfo paramsInfo){
        ParamsInfo paramsInfoOld=getParams();
        boolean ok=false;
        if(paramsInfoOld==null){
            ok=insertParams(paramsInfo);
        }else{
            ok=updateParams(paramsInfo);
        }
        return ok;
    }


}
